package intermedia;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Set;

public class ProduceCode {

        private Graph graph;
        private String path;

        public ProduceCode(Graph graph, String path) throws IOException {
                this.graph = graph;
                this.path = path;

                produceGraph();

        }

        private void produceGraph() throws IOException {
                File file = new File(path);

                if (!file.exists())
                        file.createNewFile();

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);

                //TODO Verifica se graph ou digraph
                bw.write("graph " + graph.getId() + "{\n");

                if( graph.getAttributes().attributes.size() > 0 )
                {
                        bw.write("graph [");
                        
                        Set<String> keys = graph.getAttributes().attributes
                                        .keySet();

                        for (String key : keys) {
                                bw.write(key + "=");
                                bw.write(graph.getAttributes().attributes.get(key));
                                bw.write(" ");
                        }
                        
                        bw.write("];\n");
                }
                produceNodeIds(bw);
                produceEdgeStmts(bw);

                bw.write("}");
                bw.close();

        }

        private void produceEdgeStmts(BufferedWriter bw) throws IOException {
                graph.setEdgesVisited(false);

                for (Node node : graph.getNodes()) {
                        for (Edge e : node.getEdges()) {
                                if (!e.visited) {
                                        bw.write(e.src.getId());
                                        if (e instanceof UndirectedEdge)
                                                bw.write("--");
                                        else
                                                bw.write("->");

                                        bw.write(e.dst.getId());

                                        e.visited = true;

                                        if (e.getAttributes().attributes.size() > 0) {
                                                bw.write("[");

                                                Set<String> keys = e.getAttributes().attributes
                                                                .keySet();

                                                for (String key : keys) {
                                                        bw.write(key + "=");
                                                        bw.write(e.getAttributes().attributes.get(key));
                                                        bw.write(" ");
                                                }

                                                bw.write("]");
                                        }
                                        bw.write(";\n");
                                }
                        }
                }
        }

        private void produceNodeIds(BufferedWriter bw) throws IOException {

                for (Node node : graph.getNodes()) {

                        if (node.getAttributes().attributes.size() > 0) {
                                bw.write(node.getId() + "[");

                                Set<String> keys = node.getAttributes().attributes.keySet();

                                for (String key : keys) {
                                        bw.write(key + "=");
                                        bw.write(node.getAttributes().attributes.get(key));
                                        bw.write(" ");
                                }

                                bw.write("];\n");
                        }
                }

        }

}